package com.xiaobo.client;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import com.xiaobo.util.HttpUtil;

import android.app.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class Login_Zc extends Activity {
	// 以下三变量在DataInit中初始化
	private String telId;

	private String account;// 用户帐号

	private String userKey;
	private String jgjc = "0";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Window win = getWindow();
		win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.login_zc);
		// 初始化本表单数据

		Intent intent = this.getIntent();
		Bundle bundle = intent.getExtras();

		account = bundle.getString("account");// 用户名称
		telId = bundle.getString("telId");// 人员姓名
		userKey = bundle.getString("userKey");

		// 显示标题
		TextView v_title = (TextView) findViewById(R.id.web_title);
		v_title.setText("您所在模块[用户注册]");
		TextView v_account = (TextView) findViewById(R.id.userid);
		v_account.setText(account);
		TextView v_telId = (TextView) findViewById(R.id.telid);
		v_telId.setText(telId);

		Button bt1 = (Button) findViewById(R.id.index);
		bt1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				SaveCx();
			}
		});
		Button bt2 = (Button) findViewById(R.id.exit);
		bt2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Login_Zc.this.finish();
			}
		});
		RadioGroup m_RadioGroup = (RadioGroup) this
				.findViewById(R.id.RadioGroup01);
		m_RadioGroup.setOnCheckedChangeListener(m_RadioGroupChangeListener);

	}

	private RadioGroup.OnCheckedChangeListener m_RadioGroupChangeListener = new RadioGroup.OnCheckedChangeListener() {
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			// TODO Auto-generated method stub

			if (checkedId == R.id.RadioButton01) {
				jgjc = "2";
			}
			if (checkedId == R.id.RadioButton02) {
				jgjc = "4";
			}
			if (checkedId == R.id.RadioButton03) {
				jgjc = "6";
			}
			if (checkedId == R.id.RadioButton04) {
				jgjc = "8";
			}
		}
	};

	private void SaveCx() {

		if (jgjc.equals("0")) {
			Toast toast = Toast.makeText(getApplicationContext(),
					"请选择正确的机关级次！谢谢", Toast.LENGTH_LONG);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();

		} else {

			String url = HttpUtil.LoginZc;
			String result = "-1";
			HttpPost req = new HttpPost(url);
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("account", account));
			params.add(new BasicNameValuePair("jgjc", jgjc));
			params.add(new BasicNameValuePair("telId", telId));
			try {
				req.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

				result = HttpUtil.getStringByPost(req);

			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();

			}
			System.out.println("ssafdsafdsfs="+result);
			if (result.equals("1")) {
				Toast toast = Toast.makeText(getApplicationContext(),
						"恭喜您！注册成功，请等待管理员审批...", Toast.LENGTH_LONG);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				Login_Zc.this.finish();
			} else {
				Toast toast = Toast.makeText(getApplicationContext(),
						"注册失败，可能是网络问题，请过段时间再试！", Toast.LENGTH_LONG);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				Login_Zc.this.finish();
			}
		}
	}
}
